
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#define swap(type, a, b)    do {type x=a; a=b; b=x;} while(0)

#define WEEK_LEN 7

#define STAGE_MAX WEEK_LEN

#define ERR_COUNT 5     //回答错误n次后公布答案

char *weeks[]= {
    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
};

char *ch[]= {
    "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"
};

int ques[7] = {0};

void gen_que(){
    int i;
    for (i=0; i<WEEK_LEN; ++i) {
        ques[i] = i;
    }

    for (i=WEEK_LEN-1; i>0; --i) {
        int j = rand() % (i+1);
        swap(int, ques[i], ques[j]);
    }

}

char * word_to_upper(char * dst, const char * src)
{
    char * c = dst;
    while (*c++ = toupper(*src++))
        ;

    return dst;
}

int main()
{
    int i;
    int stage, ind;
    int ecount;

    int toshow;

    int res[STAGE_MAX] = {0};

    char word[16];
    char input[16], ans[16];

    time_t s,e;

    s = time(NULL);
    srand(s);

    gen_que();

    printf("请输入星期的英文单词,单词不区分大小写.\n");
    for (stage = 0; stage < STAGE_MAX; ++stage) {
        ind = ques[stage];

        word_to_upper(word, weeks[ind]);

        ecount =0;
        do {
            printf("%s: ", ch[ind]);

            scanf("%15s", input);

            word_to_upper(ans, input);
            if (0 != strcmp(ans, word)) {

                if (++ecount > ERR_COUNT) {
                    printf("%s是%s\n", ch[ind], weeks[ind]);
                    break;

                } else {
                    printf("回答错误,要查看正确的答案吗?...0-否/1-是: ");
                    scanf("%d", &toshow);

                    if (toshow == 1) {
                        printf("%s是%s\n", ch[ind], weeks[ind]);
                        break;
                    }
                }


            } else {
                res[ind] = 1;
            }


        } while (0 != strcmp(ans, word));

    }

    e = time(NULL);

    printf("end! \n");
    printf("星期\t %-10s\t答案\n", "en");
    printf("--------------------------------\n");
    int r = 0;

    for (i=0; i<STAGE_MAX; ++i) {
        //ind = ques[i];
        //jif (res[ind] == 1)
        if (res[i] == 1)
            r++;

        printf("%s\t %-10s\t%s\n", ch[i], weeks[i], res[i] == 1? "O": "X");
    }
    printf("--------------------------------\n");

    printf("共%d道题,回答正确:%d, 共用时%.1fs\n", STAGE_MAX, r, (double)e - s);
}
